package serverdata.ch.home.www.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;

public class ServerPrincipal implements Runnable {

	private ServerSocket server;
	private int i;
	private List<ClientHosting> listaclient;
	private Context context;
	private PreparedInterface preparedInterface;
	
	public ServerPrincipal(int port) {
		
		try {
			server = new ServerSocket(port);
			i=1;
			listaclient = new ArrayList<ClientHosting>();
			
			
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	
	}
	
	
	
	
	public void run() {

		while(i<2){
			try {
				
				
				ClientHosting c = new ClientHosting(server.accept(),listaclient);
				c.addPreparedInterface(preparedInterface.getNewInstance());
				System.out.println("connesso");
				listaclient.add(c);
				new Thread(c).start();
			} catch (IOException e) {
				break;
			}
		}
		
		
	}
	
	public boolean stopServer(){
		try {
			i=2;
			server.close();
			return true;
		} catch (IOException e) {
			return false;
		}
	}
	
	public void setPreparedInterface(PreparedInterface p){
		preparedInterface = p;
	}

}
